﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using System.Linq;

namespace V2TEK.NET.SH.Silverlight.Charts.Core
{

    public class DependencyKeyValuePair<TKey, TValue> : DependencyObject
    {


        public TKey Key
        {
            get { return (TKey)GetValue(KeyProperty); }
            set { SetValue(KeyProperty, value); }
        }

        // Using a DependencyProperty as the backing store for Key.  This enables animation, styling, binding, etc...
        public static readonly DependencyProperty KeyProperty =
            DependencyProperty.Register("Key", typeof(TKey), typeof(DependencyKeyValuePair<TKey, TValue>), null);



        public TValue Value
        {
            get { return (TValue)GetValue(ValueProperty); }
            set { SetValue(ValueProperty, value); }
        }

        public static readonly DependencyProperty ValueProperty =
            DependencyProperty.Register("Value", typeof(TValue), typeof(DependencyKeyValuePair<TKey, TValue>), null);

    }

    public class DependencyDictionary<TKey, TValue> : ObservableCollection<DependencyKeyValuePair<TKey, TValue>>
    {
        public void Add(TKey key, TValue value)
        {
            this.Add(new DependencyKeyValuePair<TKey, TValue>() { Key = key, Value = value });
        }

        public TValue this[TKey key]
        {
            get
            {
                DependencyKeyValuePair<TKey, TValue> data = this.FirstOrDefault(c => c.Key.Equals(key));
                if (data != null)
                    return data.Value;
                else
                    return default(TValue);
            }
            set
            {
                DependencyKeyValuePair<TKey, TValue> data = this.FirstOrDefault(c => c.Key.Equals(key));
                //int index = this.IndexOf(data);
                if (data != null)
                {
                    if (!object.Equals(value, data.Value))
                    {
                        data.Value = value;
                    }
                }
                else
                {
                    this.Add(key, value);
                }
            }
        }

        public bool ContainsKey(TKey key)
        {
            foreach (var item in this.Items)
            {
                if (object.Equals(item.Key, key))
                {
                    return true;
                }
            }
            return false;
        }
    }
}
